﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;

/// <summary>
///ServiceHelper 的摘要说明
/// </summary>
public class ServiceHelper
{

    // Fields
    public static StringBuilder sb = new StringBuilder();

    // Methods
    public static string BuildColumnXML(string classname, string table, DataTable dataTable, string nameSpace)
    {
        sb = new StringBuilder();
        sb.AppendFormat("<Table Name=\"{0}\">\n", table);
        foreach (DataRow row in dataTable.Rows)
        {
            BuilderColumnDesc(row, sb);
        }
        sb.AppendFormat("</Table>", new object[0]);
        return sb.ToString();
    }

    public static string BuildDao(string classname, string table, DataTable dataTable, string nameSpace)
    {
        sb = new StringBuilder();
        sb.AppendLine("using System;");
        sb.AppendLine("using System.Collections.Generic;");
        sb.AppendLine("using System.Linq;");
        sb.AppendLine("using System.Text;");
        sb.AppendFormat("using {0}.Dao.ViewModel;\r\n", nameSpace);
        sb.AppendLine("using AutoMapper;");
        sb.AppendFormat("namespace {0}.Dao", nameSpace);
        sb.AppendLine("{\r\n");
        sb.AppendFormat("   public class {0}Dao", classname);
        sb.AppendLine("     {\r\n");
        sb.AppendLine();
        sb.AppendFormat("   public  {0}Dao()", classname);
        sb.AppendLine("   {\r\n ");
        sb.AppendFormat("       Mapper.CreateMap<{0}, {1}Model>();\r\n", table, classname);
        sb.AppendLine("}");
        sb.AppendFormat("     public List<{0}> GetList(int startIndex, int maxRow, out int recordcount)", table);
        sb.AppendLine();
        sb.AppendLine("     {\r\n");
        sb.AppendLine();
        sb.AppendFormat("       using(YXQManageEntities edm=new YXQManageEntities())\r\n");
        sb.AppendLine("     {\r\n");
        sb.AppendFormat("         recordcount = edm.{0}.Count();\r\n", table);
        sb.AppendLine();
        sb.AppendFormat("         return edm.{0}.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();\r\n", table);
        sb.AppendLine();
        sb.AppendLine("         }\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine();
        sb.AppendFormat("   public {0}Model GetOneById(int id)\r\n", classname);
        sb.AppendLine();
        sb.AppendLine("   {\r\n");
        sb.AppendLine();
        sb.AppendFormat("       using( YXQManageEntities edm=new YXQManageEntities())\r\n " );
        sb.AppendLine("   {\r\n");
        sb.AppendFormat("      var query= edm.{0}.FirstOrDefault(s => s.Id == id);\r\n", table);
        sb.AppendFormat("     return  Mapper.Map<{0}, {1}Model>(query);", table, classname);
        sb.AppendLine();
        sb.AppendLine("   }\r\n");
        sb.AppendLine("   }\r\n");
        sb.AppendLine();
        sb.AppendFormat("   public int Save({0}Model obj)", classname);
        sb.AppendLine();
        sb.AppendLine("   {\r\n");
        sb.AppendLine();
        sb.AppendFormat("       using( YXQManageEntities edm=new YXQManageEntities())\r\n", new object[0]);
        sb.AppendLine("   {\r\n");
        sb.AppendFormat("       var query = edm.{0}.FirstOrDefault(s => s.Id == obj.Id);\r\n", table);
        sb.AppendLine();
        sb.AppendLine("         if (query == null)\r\n");
        sb.AppendLine("         {\r\n");
        sb.AppendLine();
        sb.AppendFormat("          query = new {0}();\r\n", table);
        sb.AppendLine();
        sb.AppendFormat("         edm.AddTo{0}(query);\r\n", table);
        sb.AppendLine();
        sb.AppendLine("         }\r\n");
        sb.AppendLine();
        foreach (DataRow row in dataTable.Rows)
        {
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("          query.{0}=obj.{0};\n", row["cname"]);
                sb.AppendLine();
            }
        }
        sb.AppendLine("       edm.SaveChanges();\r\n");
        sb.AppendLine("       return query.Id;\r\n");
        sb.AppendLine();
        sb.AppendLine("   }\r\n");
        sb.AppendLine("         }\r\n");
        sb.AppendLine();
        sb.AppendFormat("   public int DelById(int id)\r\n");
        sb.AppendLine("     {\r\n");
        sb.AppendLine();
        sb.AppendFormat("       using( YXQManageEntities edm=new YXQManageEntities())\r\n", new object[0]);
        sb.AppendLine("   {\r\n");
        sb.AppendFormat("       var query = edm.{0}.FirstOrDefault(s => s.Id == id);\r\n", table);
        sb.AppendLine();
        sb.AppendLine("          if (query != null)");
        sb.AppendLine();
        sb.AppendLine("            {\r\n");
        sb.AppendLine();
        sb.AppendLine("               edm.DeleteObject(query);\r\n");
        sb.AppendLine();
        sb.AppendLine("           }\r\n");
        sb.AppendLine();
        sb.AppendLine("         return edm.SaveChanges();\r\n");
        sb.AppendLine();
        sb.AppendLine("     }\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine();
        sb.AppendLine("   }\r\n");
        sb.AppendLine(" }\r\n");
        return sb.ToString();
    }

    public static string BuildDETAILASPX(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder();
        sb.AppendFormat("<%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/manager/Manage.Master\" AutoEventWireup=\"true\" CodeBehind=\"{0}.aspx.cs\" Inherits=\"YXQ.Manager.manager.{0}\" %>", classname + "Detail\r\n");
        sb.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"head\" runat=\"server\"></asp:Content>\r\n");
        sb.AppendLine("<asp:Content ID=\"Content2\" ContentPlaceHolderID=\"ContentPlaceHolder1\" runat=\"server\">\r\n");
        sb.AppendLine("<input type=\"hidden\" runat=\"server\" id=\"hfId\"/>\r\n");
        sb.AppendLine(" <table  class=\"tbs_table\">\r\n");
        sb.AppendLine(" <tr><td colspan=\"4\"  class=\"htitle\">此行详情标题</td></tr>\r");
        //int num = 0;
        foreach (DataRow row in dataTable.Rows)
        {
            
             
                sb.AppendLine("<tr>");
             
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("   <td class=\"tdtitle\">{0}</td>", row["descption"]);
                sb.AppendFormat("   <td class=\"tddata\"><input type=\"text\" runat=\"server\" id=\"txt{0}\" DataField=\"txt{0}\"/></td>", row["cname"]);
                sb.AppendLine();
            }
            
                sb.AppendLine("</tr>");
            
        }
        sb.AppendLine("<tr><td colspan='4' align='center'><asp:Button ID=\"btnSave\" runat=\"server\" Text=\"保存\"  CssClass=\"bt_wb1\"  onclick=\"btnSave_Click\"/>&nbsp;&nbsp;<input type=\"button\" value=\"返回\"  class=\"bt_wb1\"  onclick=\"javascript:history.go(-1);\"/></td></tr>");
        sb.AppendLine("</table>");
        sb.AppendLine("</asp:Content>");
        return sb.ToString();
    }

    public static string BuildDETAILASPXCS(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder();
        sb = new StringBuilder();
        sb.AppendLine("using System;\r\n");
        sb.AppendLine("using System.Collections.Generic;\r\n");
        sb.AppendLine("using System.Linq;\r\n");
        sb.AppendLine("using System.Web;\r\n");
        sb.AppendLine("using System.Web.UI;\r\n");
        sb.AppendLine("using System.Web.UI.WebControls;\r\n");
        sb.AppendLine("using TravelWeb.Dao;\r\n");
        sb.AppendLine("using TravelWeb.Service;\r\n");
        sb.AppendLine("using TravelWeb.Common.Helper;\r\n");
        sb.AppendLine("using TravelWeb.Dao.ViewModel;\r\n");
        sb.AppendLine("namespace YXQ.Manager.manager");
        sb.AppendLine("{\r\n");
        sb.AppendFormat("    public partial class {0}Detail : BasePage", classname);
        sb.AppendLine("      {\r\n");
 
        sb.AppendFormat("      {0}Model model=new {0}Model();\r\n", classname);
        sb.AppendLine("        protected void Page_Load(object sender, EventArgs e)");
        sb.AppendLine("        {\r\n");
        sb.AppendLine("             if(!IsPostBack)");
        sb.AppendLine("             {\r\n");
        sb.AppendFormat("                  Get{0}();\r\n", classname);
        sb.AppendLine("             }\r\n");
        sb.AppendLine("        }\r\n");
        sb.AppendFormat("   private  void  Get{0}()", classname);
        sb.AppendLine("     {\r\n");
        sb.AppendFormat("       model = CenterService.{0}Service.GetOneById(reqID);\r\n", classname.Substring(0, 1).ToLower() + classname.Substring(1, classname.Length - 1));
        sb.AppendFormat("       if (model!=null)");
        sb.AppendLine("         {\r\n");
        foreach (DataRow row in dataTable.Rows)
        {
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("  //{0}\r\n", row["descption"]);
                sb.AppendFormat("   txt{0}.Value= model.{0}.ToString();\r\n", row["cname"]);
                sb.AppendLine();
            }
        }
        sb.AppendLine("      }\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// 保存一条数据");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// <param name=\"sender\"></param>");
        sb.AppendLine("/// <param name=\"e\"></param>");
        sb.AppendLine("      protected void btnSave_Click(object sender, EventArgs e)");
        sb.AppendLine("     {\r\n");
        sb.AppendFormat("                   model = string.IsNullOrEmpty(hfId.Value) ? new {0}Model() : CenterService.{1}Service.GetOneById(Convert.ToInt32(hfId.Value));\r\n", classname, classname.Substring(0, 1).ToLower() + classname.Substring(1, classname.Length - 1));
        foreach (DataRow row2 in dataTable.Rows)
        {
            if (row2["isnullable"].ToString() == "1")
            {
                string str = row2["typename"].ToString();


                switch (str)
                {
                    case "int":
                        sb.AppendFormat("  //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("  model.{0}= Convert.ToInt32(txt{0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                    case "decimal":
                    case "money":
                        sb.AppendFormat("  //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}= Convert.ToDecimal( txt{0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                    case "datetime":
                        sb.AppendFormat("   //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}=Convert.ToDateTime( txt{0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;

                    case "bit":
                        sb.AppendFormat("   //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}=Convert.ToBoolean(txt{0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                    default:
                        sb.AppendFormat("  //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}=txt{0}.Value;\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                }


            }
        }
        sb.AppendFormat(" if (CenterService.{0}Service.Save(model) > 0)", classname.Substring(0, 1).ToLower() + classname.Substring(1, classname.Length - 1));
        sb.AppendLine("    {\r\n");
        sb.AppendFormat("       JsHelper.AlertAndRedirect(this,\"数据保存成功！\", \"CommonDataList.aspx?TB={0}\");\r\n", table);
        sb.AppendLine("     }\r\n");
        sb.AppendLine("  }\r\n");
        sb.AppendLine("  }\r\n");
        sb.AppendLine("  }\r\n");
        return sb.ToString();
    }

    public static string BuildDETAILASPXDESIGN(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder();
        sb.AppendLine("namespace YXQ.Manager.manager {\r\n");
        sb.AppendFormat("public partial class {0}Detail ", classname);
        sb.Append("{\r\n");
        foreach (DataRow row in dataTable.Rows)
        {
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("   protected global::System.Web.UI.HtmlControls.HtmlInputText txt{0};\r\n", row["cname"]);
                sb.AppendLine();
            }
        }
        sb.Append("     }\r\n");
        sb.Append("}\r\n");
        return sb.ToString();
    }

    public static string BuildDETAILSiteASPXCS(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder(); 
        sb.AppendLine("using System;");
        sb.AppendLine("using System.Collections.Generic;");
        sb.AppendLine("using System.Linq;");
        sb.AppendLine("using System.Web;");
        sb.AppendLine("using System.Web.UI;");
        sb.AppendLine("using System.Web.UI.WebControls;");
        sb.AppendLine("using ERP.Dao;");
        sb.AppendLine("using ERP.Service.ImpService;");
        sb.AppendLine("using ERP.Service;");
        sb.AppendLine("using ERP.Dao.ViewModel;");
        sb.AppendLine("using ERP.Common.Helpers;");
        sb.AppendFormat("public partial class ERPService_{0}_Detail : BasePage", table);
        sb.AppendLine("{\r\n");
        sb.AppendFormat("      {0}Model model=new {0}Model();\r\n", classname);
        sb.AppendLine("        protected void Page_Load(object sender, EventArgs e)");
        sb.AppendLine("        {\r\n");
        sb.AppendLine("             if(!IsPostBack)");
        sb.AppendLine("             {\r\n");
        sb.AppendLine("                    hfId.Value=reqID.ToString();");
        sb.AppendFormat("                  Get{0}();\r\n", classname);
        sb.AppendLine("             }\r\n");
        sb.AppendLine("        }\r\n");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// 获取一条数据");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// <param name=\"sender\"></param>");
        sb.AppendLine("/// <param name=\"e\"></param>");
        sb.AppendFormat("   private  void  Get{0}()", classname);
        sb.AppendLine("     {\r\n");
        sb.AppendFormat("       model = CenterService.{0}Service.GetOneById(reqID);\r\n", classname.Substring(0, 1).ToLower() + classname.Substring(1, classname.Length - 1));
        sb.AppendFormat("       if (model!=null)", new object[0]);
        sb.AppendLine("         {\r\n");
        foreach (DataRow row in dataTable.Rows)
        {
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("  //{0}\r\n", row["descption"]);
                sb.AppendFormat("   {0}.Value= model.{0}.ToString();\r\n", row["cname"]);
                sb.AppendLine();
            }
        }
        sb.AppendLine("      }\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// 保存一条数据");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// <param name=\"sender\"></param>");
        sb.AppendLine("/// <param name=\"e\"></param>");
        sb.AppendLine("      protected void btnSave_Click(object sender, EventArgs e)");
        sb.AppendLine("     {\r\n");
        sb.AppendFormat("                   model = string.IsNullOrEmpty(hfId.Value) ? new {0}Model() : CenterService.{1}Service.GetOneById(Convert.ToInt32(hfId.Value));\r\n", classname, classname.Substring(0, 1).ToLower() + classname.Substring(1, classname.Length - 1));
        foreach (DataRow row2 in dataTable.Rows)
        {
            if (row2["isnullable"].ToString() == "1")
            {
                string str = row2["typename"].ToString();


                switch (str)
                {
                    case "int":
                        sb.AppendFormat("  //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("  model.{0}= Convert.ToInt32({0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                    case "decimal":
                    case "money":
                        sb.AppendFormat("  //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}= Convert.ToDecimal( {0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                    case "datetime":
                        sb.AppendFormat("   //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}=Convert.ToDateTime( {0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;

                    case "bit":
                        sb.AppendFormat("   //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}=Convert.ToBoolean({0}.Value);\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                    default:
                        sb.AppendFormat("  //{0}\r\n", row2["descption"]);
                        sb.AppendFormat("     model.{0}={0}.Value;\r\n", row2["cname"]);
                        sb.AppendLine();
                        break;
                }


            }
        }
        sb.AppendFormat(" if (CenterService.{0}Service.Save(model) > 0)", classname.Substring(0, 1).ToLower() + classname.Substring(1, classname.Length - 1));
        sb.AppendLine("    {\r\n");
        sb.AppendFormat("       this.AlterMsgAndRedirect(\"数据保存成功！\", \"CommonDataList.aspx?TB={0}\");\r\n", table);
        sb.AppendLine("     }\r\n");
        sb.AppendLine("  }\r\n");
        sb.AppendLine("  }\r\n");
        return sb.ToString();
    }

    public static string BuildDSiteViewASPXCS(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder();
        sb = new StringBuilder();
        sb.AppendLine("using System;");
        sb.AppendLine("using System.Collections.Generic;");
        sb.AppendLine("using System.Linq;");
        sb.AppendLine("using System.Web;");
        sb.AppendLine("using System.Web.UI;");
        sb.AppendLine("using System.Web.UI.WebControls;");
        sb.AppendLine("using ERP.Dao;");
        sb.AppendLine("using ERP.Service.ImpService;");
        sb.AppendLine("using ERP.Service;");
        sb.AppendLine("using ERP.Dao.ViewModel;");
        sb.AppendLine("using ERP.Common.Helpers;");
        sb.AppendFormat("public partial class ERPService_{0}_View : BasePage", table);
        sb.AppendLine("{\r\n");
        sb.AppendFormat("      public {0}Model model=new {0}Model();\r\n", classname);
        sb.AppendLine("        protected void Page_Load(object sender, EventArgs e)");
        sb.AppendLine("        {\r\n");
        sb.AppendLine("             if(!IsPostBack)");
        sb.AppendLine("             {\r\n");
        sb.AppendFormat("                  Get{0}();\r\n", classname);
        sb.AppendLine("             }\r\n");
        sb.AppendLine("        }\r\n");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// 获取一条数据");
        sb.AppendLine("/// <summary>");
        sb.AppendLine("/// <param name=\"sender\"></param>");
        sb.AppendLine("/// <param name=\"e\"></param>");
        sb.AppendFormat("   private  void  Get{0}()", classname);
        sb.AppendLine("     {\r\n");
        sb.AppendFormat("       model = CenterService.{0}Service.GetOneById(reqID);\r\n", classname.Substring(0, 1).ToLower() + classname.Substring(1, classname.Length - 1));
        sb.AppendLine("      }\r\n");
        sb.AppendLine("  }\r\n");
        return sb.ToString();
    }

    private static void BuilderColumnDesc(DataRow dr, StringBuilder sb)
    {
        sb.AppendFormat("<Col Name=\"{0}\" SourceName=\"{1}\" ></Col>\n", dr["cname"], dr["descption"]);
    }

    private static void BuilderGetSet(DataRow dr, StringBuilder sb)
    {
        string str = string.Empty;
        switch (dr["typename"].ToString())
        {
            case "int":
                str = "int";
                break;

            case "decimal":
            case "money":
                str = "decimal";
                break;

            case "varchar":
            case "nchar":
            case "nvarchar":
            case "text":
                str = "string";
                break;

            case "datetime":
            case "date":
                str = "DateTime";
                break;

            case "bit":
                str = "bool";
                break;
        }
        if ((dr["isnullable"].ToString() == "1") && (str != "string"))
        {
            str = str + "?";
        }
        sb.AppendFormat("///<summary>\r\n");
        sb.AppendFormat("///{0}\r\n", dr["descption"]);
        sb.AppendFormat("///</summary>\r\n");
        sb.AppendFormat("    public {0} {1} ", str, dr["cname"]);
        sb.Append("{ get; set; }\r\n");
        sb.AppendLine();
    }

    public static string BuildIservice(string classname, string table, string nameSpace)
    {
        sb = new StringBuilder();
        sb.AppendLine("using System;\r\n");
        sb.AppendLine("using System.Collections.Generic;\r\n");
        sb.AppendLine("using System.Linq;\r\n");
        sb.AppendLine("using System.Text;\r\n");
        sb.AppendFormat("using {0}.Dao;\r\n", nameSpace);
        sb.AppendFormat("  using {0}.Dao.ViewModel;\r\n", nameSpace);
        sb.AppendFormat("namespace  {0}.IService\r\n", nameSpace);
        sb.AppendLine();
        sb.AppendLine("{\r\n");
        sb.AppendFormat("   public interface I{0}Service\r\n", classname);
        sb.AppendLine();
        sb.AppendLine("     {\r\n");
        sb.AppendLine();
        sb.AppendFormat("      List<{0}> GetList(int startIndex, int maxRow, out int recordcount);\r\n", table);
        sb.AppendLine();
        sb.AppendFormat("      {0}Model GetOneById(int id);\r\n", classname);
        sb.AppendLine();
        sb.AppendFormat("      int Save({0}Model obj);\r\n", classname);
        sb.AppendLine();
        sb.AppendLine("        int DelById(int id);\r\n");
        sb.AppendLine();
        sb.AppendLine("      }\r\n");
        sb.AppendLine("}\r\n");
        return sb.ToString();
    }

    public static string BuildJavascript()
    {
        return "";
    }

    public static string BuildListASPX(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder();
        sb.AppendFormat("<%@ Page  Language=\"C#\" MasterPageFile=\"~/Manager/Base.Master\" AutoEventWireup=\"true\" CodeBehind=\"{0}.aspx.cs\" Inherits=\"YXQ.Manager.manager.{0}\" %>", classname + "List");
        sb.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"head\" runat=\"server\"></asp:Content>");
        sb.AppendLine("<asp:Content ID=\"Content2\" ContentPlaceHolderID=\"ContentPlaceHolder1\" runat=\"server\">");
        sb.AppendLine(" <table width=\"100%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#d5dfe8\">");
        sb.AppendLine("<tr  bgcolor=\"#eef3f7\">");
        foreach (DataRow row in dataTable.Rows)
        {
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("   <td>{0}</td>", row["cname"]);
                sb.AppendLine();
            }
        }
        sb.AppendLine("</tr>");
        sb.AppendFormat("<asp:Repeater ID=\"rep{0}\" runat=\"server\"><ItemTemplate>", classname);
        sb.AppendLine("<tr>");
        foreach (DataRow row2 in dataTable.Rows)
        {
            if (row2["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("   <td><%#Eval(\"{0}\") %></td>", row2["cname"]);
            }
        }
        sb.AppendLine("</tr>");
        sb.AppendLine("  </ItemTemplate></asp:Repeater></table>");
        sb.AppendLine("<Jdom:AspNetPager ID=\"pager\" PageSize=\"10\" CssClass=\"pagenum\" runat=\"server\" AlwaysShow=\"true\" PrevPageText=\"<span>上一页</span>\" FirstPageText=\"<span>首页</span>\" LastPageText=\"<span>尾页</span>\" NextPageText=\"<span>下一页</span>\"></Jdom:AspNetPager>");
        sb.AppendLine("</asp:Content>");
        return sb.ToString();
    }

    public static string BuildListASPXCS(string classname, string table)
    {
        sb = new StringBuilder();
        sb.AppendLine("using System;\r\n");
        sb.AppendLine("using System.Collections.Generic;\r\n");
        sb.AppendLine("using System.Linq;\r\n");
        sb.AppendLine("using System.Web;\r\n");
        sb.AppendLine("using System.Web.UI;\r\n");
        sb.AppendLine("using System.Web.UI.WebControls;\r\n");
        sb.AppendLine("using TravelWeb.Dao;\r\n");
        sb.AppendLine("using TravelWeb.Service;\r\n");
        sb.AppendLine("namespace YXQ.Manager.manager");
        sb.AppendLine("{\r\n");
        sb.AppendFormat("    public partial class {0}List : BaseAdminPage", classname);
        sb.AppendLine("    {\r\n");
        sb.AppendLine("        protected void Page_Load(object sender, EventArgs e)");
        sb.AppendLine("        {\r\n");
        sb.AppendFormat("            Get{0}List();\r\n", classname);
        sb.AppendLine("        }\r\n");
        sb.AppendFormat("        public  void  Get{0}List()", classname);
        sb.AppendLine("        {\r\n");
        sb.AppendLine("            int recordcount = 0;\r\n");
        sb.AppendFormat("            List<{0}> {1}s = CenterService.{1}Service.GetList(AdminUser.Id,pager.StartRecordIndex-1,pager.PageSize,out recordcount);\r\n", table, classname.ToLower(), classname);
        sb.AppendLine("           pager.RecordCount = recordcount;\r\n");
        sb.AppendFormat("            rep{0}.DataSource = {1}s;\r\n", classname, classname.ToLower());
        sb.AppendFormat("            rep{0}.DataBind();\r\n", classname);
        sb.AppendLine("        }\r\n");
        sb.AppendLine("    }\r\n");
        sb.AppendLine("}\r\n");
        return sb.ToString();
    }

    public static string BuildListDesign(string classname, string table)
    {
        sb = new StringBuilder();
        sb.AppendLine("namespace YXQ.Manager.manager {\r\n");
        sb.AppendFormat("   public partial class {0}List  ", classname);
        sb.AppendLine("{\r\n");
        sb.AppendFormat("      protected global::System.Web.UI.WebControls.Repeater rep{0};\r\n", classname);
        sb.AppendLine("\t\t protected global::Wuqi.Webdiyer.AspNetPager pager;\r\n");
        sb.AppendLine("    }\r\n");
        sb.AppendLine("}\r\n");
        return sb.ToString();
    }

    public static string BuildModel(string classname, string table, DataTable dataTable, string nameSpace)
    {
        sb = new StringBuilder();
        sb.AppendLine("using System;\r\n");
        sb.AppendFormat("namespace {0}.Dao.ViewModel", nameSpace);
        sb.AppendLine("{\r\n");
        sb.AppendLine();
        sb.AppendFormat("  public class {0}Model", classname);
        sb.AppendLine("      {\r\n");
        sb.AppendLine();
        foreach (DataRow row in dataTable.Rows)
        {
            BuilderGetSet(row, sb);
        }
        sb.AppendLine();
        sb.AppendLine("       }\r\n");
        sb.AppendLine();
        sb.AppendLine("}\r\n");
        return sb.ToString();
    }

    public static string BuildService(string classname, string table, string NameSpace)
    {
        sb = new StringBuilder();
        sb.AppendLine("using System;\r\n");
        sb.AppendLine("using System.Collections.Generic;\r\n");
        sb.AppendLine("using System.Linq;\r\n");
        sb.AppendLine("using System.Text;\r\n");
        sb.AppendFormat("using {0}.Dao;\r\n", NameSpace);
        sb.AppendFormat("using {0}.IService;\r\n", NameSpace);
        sb.AppendFormat("using   {0}.Dao.ViewModel;\r\n", NameSpace);
        sb.AppendFormat("namespace {0}.Service \r\n", NameSpace);
        sb.AppendLine();
        sb.AppendLine("{\r\n");
        sb.AppendFormat("  public class  {0}Service : I{0}Service\r\n", classname);
        sb.AppendLine();
        sb.AppendLine("    {\r\n");
        sb.AppendLine();
        sb.AppendFormat("     {0}Dao dao=new {0}Dao();\r\n", classname);
        sb.AppendLine();
        sb.AppendLine();
        sb.AppendFormat("    #region I{0}Service 成员\r\n", classname);
        sb.AppendLine();
        sb.AppendFormat("   public List<{0}> GetList(int startIndex, int maxRow, out int recordcount)\r\n", table);
        sb.AppendLine("     {\r\n");
        sb.AppendLine("       return dao.GetList(startIndex, maxRow,out recordcount);\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine();
        sb.AppendLine("     #endregion\r\n");
        sb.AppendLine();
        sb.AppendFormat("    #region I{0}Service 成员\r\n", classname);
        sb.AppendLine();
        sb.AppendFormat("   public int Save({0}Model obj)\r\n", classname);
        sb.AppendLine("     {\r\n");
        sb.AppendLine("       return dao.Save(obj);\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine();
        sb.AppendLine("     #endregion\r\n");
        sb.AppendLine();
        sb.AppendFormat("   #region I{0}Service 成员\r\n", classname);
        sb.AppendLine();
        sb.AppendFormat("   public {0}Model GetOneById(int id)\r\n", classname);
        sb.AppendLine("     {\r\n");
        sb.AppendLine("        return dao.GetOneById(id);\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine();
        sb.AppendLine("     #endregion");
        sb.AppendLine();
        sb.AppendFormat("   #region I{0}Service 成员", classname);
        sb.AppendLine();
        sb.AppendFormat("   public int DelById(int id)\r\n", new object[0]);
        sb.AppendLine("     {\r\n");
        sb.AppendLine("        return dao.DelById(id);\r\n");
        sb.AppendLine("     }\r\n");
        sb.AppendLine();
        sb.AppendLine("     #endregion");
        sb.AppendLine();
        sb.AppendLine("   }\r\n");
        sb.AppendLine(" }\r\n");
        return sb.ToString();
    }

    public static string BuildSiteDETAILASPX(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder();
        sb.AppendFormat("<%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/ERPService/Base.Master\" AutoEventWireup=\"true\" CodeFile=\"{0}_Detail.aspx.cs\" Inherits=\"ERPService_{0}_Detail\" %>", table);
        sb.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"head\" runat=\"server\"></asp:Content>");
        sb.AppendLine("<asp:Content ID=\"Content2\" ContentPlaceHolderID=\"ContentPlaceHolder1\" runat=\"server\">");
        sb.AppendLine("<input type=\"hidden\" runat=\"server\" id=\"hfId\"/>");
        sb.AppendLine(" <table class=\"tableborder\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#000000\">");
        sb.AppendLine(" <tr><td clospan=\"4\">此行详情标题</td></tr>");
        int num = 0;
        foreach (DataRow row in dataTable.Rows)
        {
            num++;
            if ((num % 2) == 1)
            {
                sb.AppendLine("<tr>");
            }
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("   <td class=\"tdtitle\">{0}</td>", row["descption"]);
                sb.AppendFormat("   <td class=\"tddata\"><input type=\"text\" runat=\"server\" id=\"{0}\" DataField=\"{0}\"/></td>", row["cname"]);
                sb.AppendLine();
            }
            if ((num % 2) == 0)
            {
                sb.AppendLine("</tr>");
            }
        }
        sb.AppendLine("<tr><td clospan='4'><asp:Button ID=\"btnSave\" runat=\"server\" Text=\"保存\"  CssClass=\"btn002\"  onclick=\"btnSave_Click\"/></td></tr>");
        sb.AppendLine("</table>");
        sb.AppendLine("</asp:Content>");
        return sb.ToString();
    }

    public static string BuildSiteViewASPX(string classname, string table, DataTable dataTable)
    {
        sb = new StringBuilder();
        sb.AppendFormat("<%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/ERPService/Base.Master\" AutoEventWireup=\"true\" CodeFile=\"{0}_View.aspx.cs\" Inherits=\"ERPService_{0}_View\" %>", table);
        sb.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"head\" runat=\"server\"></asp:Content>");
        sb.AppendLine("<asp:Content ID=\"Content2\" ContentPlaceHolderID=\"ContentPlaceHolder1\" runat=\"server\">");
        sb.AppendLine("<input type=\"hidden\" runat=\"server\" id=\"hfId\"/>");
        sb.AppendLine(" <table class=\"caotable\" cellspacing=\"0\" cellpadding=\"0\" style=\"width: 100%;\">");
        sb.AppendLine(" <tr><td clospan=\"4\">此行详情标题</td></tr>");
        int num = 0;
        foreach (DataRow row in dataTable.Rows)
        {
            num++;
            if ((num % 2) == 1)
            {
                sb.AppendLine("<tr>");
            }
            if (row["isnullable"].ToString() == "1")
            {
                sb.AppendFormat("   <td class=\"tdtitle\">{0}</td>", row["descption"]);
                sb.AppendFormat("   <td class=\"tddata\"><%=model.{0}%></td>", row["cname"]);
                sb.AppendLine();
            }
            if ((num % 2) == 0)
            {
                sb.AppendLine("</tr>");
            }
        }
        sb.AppendLine("</table>");
        sb.AppendLine("</asp:Content>");
        return sb.ToString();
    }




}